package com.xh.demo.controller;

import com.xh.demo.utils.FileMergeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Name TestController
 * @Description
 * @Author wen
 * @Date 2020-06-08
 */
@Controller
@RequestMapping("/upload")
public class UploadController {

    private String filePath = "F:/IDEA_2019/temp/";

    @GetMapping("/up")
    public String up() {
        return "up";
    }

    @ResponseBody
    @PostMapping("/test")
    public String upload(@RequestParam("data") MultipartFile data, @RequestParam("name") String name,
                         @RequestParam("total") Integer total, @RequestParam("index") Integer index) throws IOException {
        data.transferTo(new File(total.equals(1) ? (filePath + name) : (filePath + index)));
        return "SUCCESS";
    }

    @ResponseBody
    @GetMapping("/merge/{name}/{total}")
    public String merge(@PathVariable("name") String name, @PathVariable("total") Integer total) throws Exception {
        FileMergeUtils.mergeFilesNio(total, filePath, "F:/IDEA_2019/" + name);
        return "SUCCESS";
    }

}
